var router = require('libs/router').router;
var pages = require('libs/pages');
var ItemMixin = require('mixins/item');
var libs = require('libs/libs');
var _user;
var api = libs.api;

function clicks(cls, rt){
    if (!cls){
        return false;
    }
    $('.'+cls).click(function(){
        router(rt)
    })
    return clicks;
}

var bindEvent = function(){
    //此处各种dom 操作
    // 提交测试commit而已
    if (!_user.info){
        SA.set('USER', _fun)
        function _fun(data){
            $('#uc_index_top_user_name').html(data.info.userName||'123')
            $('#uc_index_top_user_stat').html('')
        }
    }

    clicks('uc_order', 'uc/order')
          ('uc_coupon', 'uc/coupons')
          ('uc_edit', 'uc/edituser')
          ('uc_faq', 'uc/faq')
          ('uc_message', 'uc/message')
          ('uc_zone', 'uc/location')
          ('uc_intro', 'uc/siteIntroduce')

    $('.login_exit').click(function(){
        ui.alertBox({
            cons: [{text: '确定要退出', name: "bigCon"}],
            btns: [
                {
                    text: '取消',
                    close: true
                },
                {
                    text: '确定',
                    close: true,
                    callback: function () {
                        var postdata = {
                            login: _user.login,
                            method: 'get'
                        }
                        api.req('/unbind_wx', postdata, function(data){
                            if (wx){
                                wx.closeWindow()
                            }
                        })
                    }
                }
            ]
        })

    })
};

//演示模块
var Show = React.createClass({
    mixins: [ItemMixin],
    getInitialState: function() {
        return {
            info: false
        };
    },
    componentWillMount: function() {
        if (this.props.data){
            this.setState({
                info: this.props.data
            })
        }
    },
    render:function(){
        var _info = this.state.info
        var _add = _info.currentCommunity.cityName + _info.currentCommunity.communityName
        return (
            <div className="default_div app_index">
                <ul className="default_Picture">
                    <li className="dP_pic uc_edit">
                        <div className="dp_pic_l">
                            <img src="./images/ic_default_head.png"/>
                        </div>
                        <div className="dp_pic_name">
                            <p id="uc_index_top_user_name">{_info.userName||_info.mobile}</p>
                            <i className="iconfont icon-xiangyou"></i>
                        </div>
                    </li>
                    <li className="uc_sitephone">
                        <label>网点电话</label>
                        <a type="tel" href={"tel:"+(_info.sitePhone||'')} style={{display:"block",width:"80%",textAlign:"right"}}>{_info.sitePhone||""}</a>
                        {/*<input type="tel" defaultValue={_info.sitePhone} placeholder="请输入姓名" readOnly="readOnly"/>*/}
                        {/*<i className="iconfont icon-xiangyou"></i>*/}
                    </li>
                    <li className="uc_order">
                        <label>我的订单</label>
                        <i className="iconfont icon-xiangyou"></i>
                    </li>
                    <li className="uc_message">
                        <label>消息中心</label>
                        {function(){
                            return _info.unReadMsgTotal
                            ? <i className="popmessage">{_info.unReadMsgTotal}</i>
                            :''
                        }()}
                        <i className="iconfont icon-xiangyou"></i>
                    </li>
                    <li className="uc_coupon">
                        <label>我的卡劵</label>
                        <i className="iconfont icon-xiangyou"></i>
                    </li>
                    <li className="uc_intro">
                        <label>网点介绍</label>
                        <i className="iconfont icon-xiangyou"></i>
                    </li>
                    <li className="uc_zone">
                        <label>切换小区</label>
                        <input type="text" defaultValue={_add} placeholder="请输入姓名" readOnly="readOnly"/>
                        <i className="iconfont icon-xiangyou"></i>
                    </li>
                    <li className="uc_faq">
                        <label>常见问题</label>
                        <i className="iconfont icon-xiangyou"></i>
                    </li>
                </ul>
                <div style={{height:"0.44rem",lineHeight:"0.44rem",backgroundColor:'transparent',margin:"0.2rem 0"}}>
                    <div className="f_submit" style={{width:'100%'}}>
                        <input type="submit" defaultValue="退出登录" className="f_btn login_exit" />
                    </div>
                </div>
            </div>
        );
    }
});

function renderHtml(){
    var me = this;
    var oUser = SA.get('USER');
    api.req('/userInfo', {login: oUser.login, method: 'get'}, function(ddd){
        var _resault = {login: oUser.login, info: ddd}
        SA.set('USER', _resault)
        // console.log(_resault);
        if (!ddd.currentCommunity){
            router('uc/location')
        }
        else {
            me.main(_resault.info)
        }
    })
}

function start(name){
    return pages.new({
        boot:function(){},
        trigger:function(){
            this.libs.changeTitle('个人中心');    //更改当前页面标题
            _user = SA.get('USER')
            renderHtml.call(this)
        },
        goback: function(){
            var gnext = SA.get('_GoBack')
            if (gnext){
                router.cb = function(){
                    SA.deleter('_GoBack')
                    router(gnext)
                }
            }else{
                router.cb = function(){
                    if (wx){
                        wx.closeWindow()
                    }
                }
            }
        },
        main: function(data){
            this.render(
                <Show itemDefaultMethod={bindEvent} data={data} />,
                document.getElementById(name)
            )
        },
        end: function(){
            React.unmountComponentAtNode(document.getElementById(name))
        }
    })
}


module.exports = start;
